/*
    Geocache: some utilities for managing and visualizing geocache information
    Copyright (C) 2008  Gary Jackson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * Point.java
 */

package com.thegbomb.sphere;

/**
 *
 * @author Gary Jackson
 */
public class Point {
    public static final Point NORTH_POLE = new Point(Math.PI/2, 0);
    public static final Point SOUTH_POLE = new Point(-Math.PI/2, 0);
    public static final Point ORIGIN = new Point(0, 0);
    
    private double theta, phi;
    
    /**
     * Creates a new instance of Point
     */
    public Point(double theta, double phi) {
        assert !(Double.isInfinite(theta) && Double.isNaN(theta));
        assert !(Double.isInfinite(phi) && Double.isNaN(phi));
        
        if ((theta > Math.PI/2) || (theta < -Math.PI/2)) {
            theta = ((theta + Math.PI) % (2*Math.PI));
            if (theta <= 0) theta += 2*Math.PI;
            theta -= Math.PI;
            if ((theta >= Math.PI/2) || (theta < -Math.PI/2)) {
                theta = Math.PI - theta;
                phi += Math.PI;
            }
        }
        
        if ((phi >= Math.PI) || (phi < -Math.PI)) {
            phi = ((phi + Math.PI) % (2*Math.PI));
            if (phi <= 0) phi += 2*Math.PI;
            phi -= Math.PI;
        }
        
        this.theta = theta;
        if (Math.abs(theta) != Math.PI/2)
            this.phi = phi;
        else this.phi = 0;
    }
    
    public Point(Cartesian c) {
        this(Math.asin(c.getZ()), Math.atan2(c.getY(), c.getX()));
    }
    
    public double getTheta() {
        return this.theta;
    }
    
    public double getPhi() {
        return this.phi;
    }
    
    public boolean equals(Object o) {
        if (o instanceof Point) {
            Point p = (Point) o;
            return ((theta == p.theta) && (phi == p.phi));
        } else return false;
    }
    
    public int hashCode() {
        return this.toString().hashCode();
    }
    
    public boolean equals(Point p, double epsilon) {
        return (Math.abs(this.getTheta() - p.getTheta()) < epsilon) &&
                (Math.abs(this.getPhi() - p.getPhi()) < epsilon);
    }
    
    public String toString() {
        double thetap = theta / Math.PI;
        double phip = phi / Math.PI;
        return "(" + thetap + "*PI, " + phip + "*PI)";
    }
    
    public Point rotate(Point axis, double angle) {
        Cartesian u = new Cartesian(axis);
        
        double matrix[][] = new double[3][3];
        
        double x = u.getX(), y = u.getY(), z = u.getZ();
        double ct = Math.cos(angle), st = Math.sin(angle), omct = 1 - ct;
        
        matrix[0][0] = x*x*omct+1*ct; matrix[0][1] = x*y*omct-z*st; matrix[0][2] = x*z*omct+y*st;
        matrix[1][0] = x*y*omct+z*st; matrix[1][1] = y*y*omct+1*ct; matrix[1][2] = y*z*omct-x*st;
        matrix[2][0] = x*z*omct-y*st; matrix[2][1] = y*z*omct+x*st; matrix[2][2] = z*z*omct+1*ct;
        
        Cartesian ret = new Cartesian(this).postmultiply(matrix);
        Point pRet = new Point(ret.normalize());
        return new RotatedPoint(ret.normalize(), this);
    }
    
    public Point normal(Point p) {
        return new Point(this.getCartesian().cross(p.getCartesian()).normalize());
    }
    
    public Point cross(Point p) {
        return new Point(this.getCartesian().cross(p.getCartesian()));
    }
    
    public double dot(Point p) {
        return this.getCartesian().dot(p.getCartesian());
    }
    
    public Cartesian getCartesian() {
        return new Cartesian(this);
    }
    
    public Point antipode() {
        return new Point(-this.theta, this.phi + Math.PI);
    }
    
    public boolean isAntipodal(Point p) {
        return this.antipode().equals(p);
    }
    
    public boolean isAntipodal(Point p, double epsilon) {
        return this.antipode().equals(p, epsilon);
    }
    
    public double distance(Point f) {
        double dphi = this.phi - f.getPhi();
        double a = Math.cos(f.getTheta()) * Math.sin(dphi);
        double b = Math.cos(this.theta)*Math.sin(f.getTheta()) - Math.sin(this.theta)*Math.cos(f.getTheta())*Math.cos(dphi);
        double y = a*a + b*b;
        
        double c = Math.sin(this.theta)*Math.sin(f.getTheta());
        double d = Math.cos(this.theta)*Math.cos(f.getTheta())*Math.cos(dphi);
        double x = c + d;
        return Math.atan2(Math.sqrt(y), x);
    }
}
